﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ITAP.Core.Modelling.Primitives;

using ITAP.Core.Calculations;
using System.IO;
using ITAP.ContourBuilder.Components;
using System.Globalization;
using ITAP.Core.Utils;
using ITAP.Core.Modelling;

namespace ITAP.ContourBuilder.Operations
{
    public class OperationOpenAnz : Operation
    {
        private List<string> anz;
        private int currentIndex = 0;

        public override string ToString()
        {
            return "Открыть anz";
        }

        public override void Execute()
        {
            base.Execute();
            OpenFileDialog dialog = new OpenFileDialog();
            dialog.Filter = "Anz files (*.anz)|*.anz";
            if (dialog.ShowDialog() != DialogResult.OK)
            {
                this.Cancel();
                return;
            }

            string[] anzArray = File.ReadAllLines(dialog.FileName);
            anz = new List<string>(anzArray);
            currentIndex = 0;

            List<IPrimitive> primitives = new List<IPrimitive>();
            while (currentIndex < anz.Count)
            {
                if (anz[currentIndex] == "2 0 0 2")
                    primitives.Add(this.ReadLine());
                else if (anz[currentIndex] == "3 0 0 2")
                    primitives.Add(this.ReadCircle());
                else
                    currentIndex++;
            }

            ContourModel model = ContourElementFactory.CreateModel(primitives);
            //this.Control.ContourContainer.AddInnerComponent(model);
            //foreach (IPrimitive primitive in primitives)
            //{
            //    model.AddInnerComponent(ContourElementFactory.CreateContourElementFromPrimitive(primitive, ""));
            //}
            //this.Control.UpdateModel();
            //this.Control.ToleranceManager.Visit(this.Control.ContourContainer);
            //this.Control.UpdateModel();
            this.Exit();
            return;
        }

        public override bool CanUndo
        {
            get
            {
                return false;
            }
        }

        CultureInfo culture = new CultureInfo("en-US");
        private double ParseDouble(string str)
        {
            return double.Parse(str, culture);
        }


        private Line ReadLine()
        {
            Line line = new Line();
            line.MoveStartPoint(new ModelPoint(ParseDouble(anz[currentIndex + 1]), ParseDouble(anz[currentIndex + 2])));
            line.MoveEndPoint(new ModelPoint(ParseDouble(anz[currentIndex + 3]), ParseDouble(anz[currentIndex + 4])));
            currentIndex = currentIndex + 5;
            return line;
        }

        private Circle ReadCircle()
        {
            ModelPoint center = new ModelPoint(ParseDouble(anz[currentIndex + 1]), ParseDouble(anz[currentIndex + 2]));
            double radius = ParseDouble(anz[currentIndex + 3]);
            Angle startAngle = new Angle(ParseDouble(anz[currentIndex + 4]));
            Angle endAngle = new Angle(ParseDouble(anz[currentIndex + 5]));
            AngleInterval interval = new AngleInterval(startAngle, endAngle);
            
            ModelVector vector = new ModelVector(1, 0);
            vector.Length = radius;
            ModelVector startVector = vector.Rotate(startAngle);
            ModelVector endVector = vector.Rotate(endAngle);
            
            ModelPoint startPoint = ModelCalculations.GetOffsetPoint(center, startVector);
            ModelPoint endPoint = ModelCalculations.GetOffsetPoint(center, endVector);
            ModelVector centerVector = vector.Rotate(interval.MiddleAngle);
            ModelPoint centerPoint = ModelCalculations.GetOffsetPoint(center, centerVector);
            Circle circle = new Circle(startPoint, centerPoint, endPoint);
            currentIndex = currentIndex + 10;
            return circle;
        }
    }

}
